﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KCL.Code.Data;

namespace KCL.Summary
{
    /// <summary>
    /// Summary description for UnfinishedProducts1
    /// </summary>
    public class UnfinishedProducts1 : IHttpHandler
    {

        #region IHttpHandler Members
        public void ProcessRequest(HttpContext context)
        {
            // get parameters from query string
            var action = context.Request.Form["action"];

            var transferedFinishQuantity = 0;
            var sourceProductOrderId = -1;
            var destinationProductOrderId = -1;

            context.Response.ContentType = "application/html";
            var responseData = string.Empty;

            try
            {
                transferedFinishQuantity = Convert.ToInt32(context.Request.Form["transferedFinishQuantity"]);
                sourceProductOrderId = Convert.ToInt32(context.Request.Form["sourceProductOrderId"]);
                destinationProductOrderId = Convert.ToInt32(context.Request.Form["destinationProductOrderId"]);
                Search searchJob = new Search();

                bool isUpdated = searchJob.UpdateExchangeFinishedQuantity(destinationProductOrderId, sourceProductOrderId, transferedFinishQuantity);

                if (isUpdated == true)
                {
                    //context.Response.Write(string.Format("$('#divProduct').dialog('close'); alert('Đã chuyển thành phẩm!'); javascript:__doPostBack('ctl00$MainContent$gridUnfinishedProducts','Page${0}');", searchJob.GetTotalUnfinishedProducts() / 10));
					context.Response.Write(string.Format("$('#divProduct').dialog('close'); alert('Đã chuyển thành phẩm!'); location.reload(true);"));
                }
                else
                {
                    ////context.Response.Write(string.Format("$('#divProduct').dialog('close'); alert('Không chuyển được thành phẩm, xin thử lại lần sau!'); javascript:__doPostBack('ctl00$MainContent$gridUnfinishedProducts','Page${0}');", searchJob.GetTotalUnfinishedProducts() / 10));           
                    context.Response.Write(string.Format("$('#divProduct').dialog('close'); alert('Đã chuyển thành phẩm!');"));
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(string.Format("alert(\"Có lỗi: {0}\");", ex.Message));
                return;
            }  
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion
    }
}